package com.pzh.guli.service.edu.service.imp;

import cn.pzh.guli.service.utils.JwtInfo;
import cn.pzh.guli.service.utils.JwtUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pzh.guli.service.base.dto.CourseDto;
import com.pzh.guli.service.base.dto.MemberDto;
import com.pzh.guli.service.base.handler.HandlerException;
import com.pzh.guli.service.base.result.Result;
import com.pzh.guli.service.base.result.ResultCodeEnum;
import com.pzh.guli.service.edu.feign.EduCourseService;
import com.pzh.guli.service.edu.feign.UserMemberService;
import com.pzh.guli.service.edu.pojo.Order;
import com.pzh.guli.service.edu.mapper.OrderMapper;
import com.pzh.guli.service.edu.pojo.vo.OrderVo;
import com.pzh.guli.service.edu.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pzh.guli.service.edu.util.OrderNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xzh
 * @since 2022-07-21
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {
    @Resource
    private EduCourseService courseService;
    @Resource
    private UserMemberService userMemberService;
    @Resource
    private OrderMapper orderMapper;
    /**
     * 创建订单
     * @param courseId 课程id
     * @param request 获取会员id
     * @return
     */
    @Override
    public String createOrder(String courseId, HttpServletRequest request) throws HandlerException {
        JwtInfo memberInfoByToken = JwtUtil.getMemberInfoByToken(request);
        if(memberInfoByToken==null){ //未登录
            throw new HandlerException(ResultCodeEnum.USER_NOT_LOGIN);
        }
        String memberId = memberInfoByToken.getId();
        //判断当前用户是否已经存在当前课程订单
        QueryWrapper<Order> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        queryWrapper.eq("member_id",memberId);
        Order order = orderMapper.selectOne(queryWrapper);
        if (order!=null){ //订单存在
            return order.getOrderNo();
        }
        //查询课程信息
        CourseDto courseDto = courseService.getCourseDto(courseId);
        //查询会员信息
        MemberDto memberDto = userMemberService.getMemberDto(memberId);
        //组装信息
        Order newOrder=new Order();
        String OrderNo=OrderNoUtil.getOrderNo();
        newOrder.setOrderNo(OrderNo);
        newOrder.setCourseId(courseDto.getId());
        newOrder.setCourseTitle(courseDto.getTitle());
        newOrder.setCourseCover(courseDto.getCover());
        newOrder.setTeacherName(courseDto.getTeacherName());
        newOrder.setTotalFee(courseDto.getPrice());
        newOrder.setMemberId(memberDto.getId());
        newOrder.setMobile(memberDto.getMobile());
        newOrder.setNickname(memberDto.getNickname());
        newOrder.setStatus(0);//未支付
        newOrder.setPayType(1);//支付宝
        orderMapper.insert(newOrder);//创建
        return OrderNo;
    }
   //判断课程是否已购买
    @Override
    public boolean isBuyCourse(String courseId, HttpServletRequest request) throws HandlerException {
        JwtInfo jwtInfo = JwtUtil.getMemberInfoByToken(request);
        if (jwtInfo==null){
            return false;
        }
        String memberId = jwtInfo.getId();
        QueryWrapper<Order> orderQueryWrapper=new QueryWrapper<>();
        orderQueryWrapper.eq("course_id",courseId);
        orderQueryWrapper.eq("member_id",memberId);
        orderQueryWrapper.eq("status",1);
        Long count = orderMapper.selectCount(orderQueryWrapper);
        return count != 0;
    }
    //根据课程号获取订单信息
    @Override
    public OrderVo getOrderVoInfoByCourseId(String courseId, HttpServletRequest request) throws HandlerException {
        JwtInfo memberInfoByToken = JwtUtil.getMemberInfoByToken(request);
        if(memberInfoByToken==null){ //未登录
            throw new HandlerException(ResultCodeEnum.USER_NOT_LOGIN);
        }
        String memberId = memberInfoByToken.getId();
        //查询课程信息
        CourseDto courseDto = courseService.getCourseDto(courseId);
        //查询会员信息
        //组装OrderVo
        OrderVo orderVo=new OrderVo();
        orderVo.setSubject(courseDto.getTeacherName());
        orderVo.setAvatar(courseDto.getCover());
        orderVo.setTotalAmount(courseDto.getPrice());
        return orderVo;
    }
}
